﻿namespace Bowlus.FrontEnd.RequestRouting.ApplicationDeployment
{
    using System;

    using Bowlus.Common;
    using Bowlus.Common.Communications;

    using Microsoft.WindowsAzure.ServiceRuntime;

    public abstract class ApplicationDeploymentRequest
    {
        protected ApplicationDeploymentRequest(TenantDeployment tenantDeployment)
        {
            this.TenantDeployment = tenantDeployment;
        }

        public TenantDeployment TenantDeployment { get; private set; }

        public abstract Guid SendDeploymentRequest();

        /// <summary>
        /// </summary>
        /// <param name="response">
        /// The response.
        /// </param>
        /// <param name="nextMessageId">
        /// The next message id to wait for. Null if no message is expected. Thus do not wait for anything else
        /// </param>
        /// <returns>
        /// True or false indicating success or failure.
        /// </returns>
        public abstract bool CanUpdateConfiguration(BowlusMessage response, out Guid nextMessageId);

        protected BowlusMessage ConstructMessage(BowlusMessageType messageType)
        {
            return new BowlusMessage
                {
                    MessageType = messageType,
                    MessageId = Guid.NewGuid(),
                    Sender = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[Settings.FrontEndRoleName].IPEndpoint,
                    Body = this.TenantDeployment.Serialize()
                };
        }
    }
}
